<?
header('Access-Control-Allow-Origin: *');
require_once('../../db/db.php');
$db = new DBConnection('dbname=assclap_realty port=5432 user=assclap_assclap password=fibo1235');
if (isset($_GET['polygon']))
{
	
	$name = addslashes($_GET['name']);
	$color = addslashes($_GET['color']);
	$description = addslashes($_GET['description']);
	$polygon = addslashes($_GET['polygon']);
	// need to transform this:
	// (33.75453077057167, -84.26350567102052)(33.75495894053213, -84.25938579797365)(33.752140115659984, -84.2597720360718)
	// into 
	// 33.75453077057167 -84.26350567102052, 33.75495894053213 -84.25938579797365, 33.752140115659984 -84.2597720360718, 33.75453077057167 -84.26350567102052
	$polygon = str_replace(',', '', $polygon);
	$polygon = str_replace(')(', ',', $polygon);
	$polygon = str_replace('(', '', $polygon);
	$polygon = str_replace(')', '', $polygon);
	$arrPG = explode(',', $polygon);
	array_push($arrPG, $arrPG[0]);
	$polygon = implode(',',$arrPG);
	$sql = "select count(*) as cnt from user_polygon where name = '$name'";
	$Query = $db->CreateQuery($sql);
	$r = $Query->FetchObject();
	if($r->cnt == 0)
	{
		$sql = "INSERT INTO user_polygon (name, description, color, the_geom) 
		VALUES ('$name', '$description', '$color', GeomFromText('POLYGON(($polygon))',4326))";
	}
	else
	{
		$sql = "UPDATE property_aux set description = '$description', color = '$color', the_geom = GeomFromText('POLYGON(($polygon))',4326) name = $name";
	}
	user_error($sql);
	$db->CreateQuery($sql);
} 
?>